﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Data;
using System.Runtime.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml;
using System.Xml.Serialization;
using HT.DAL;

public class Common
{



    private readonly Dal_Common dal = new Dal_Common();
    public byte[] ReadImgStream(string spbh, int type)
    {
        byte[] bytes = null;
        if (type == 0)
        {
            bytes = GetDataByFiled("tp", "spdah", "spbh", spbh);
        }
        else
        {
            bytes = GetDataByFiled("tp", "spdah", "tmh", spbh);
        }

        return bytes;

    }

    /// <param name="filed">想获取值 字段</param>
    /// <param name="tableName">表名</param>
    /// <param name="key">条件</param>
    /// <param name="condition">条件值</param>
    /// <returns></returns>
    public byte[] GetDataByFiled(string filed, string tableName, string key, string condition)
    {
        return dal.GetDataByFiled(  filed,   tableName,   key,   condition);
    }

    #region 序列化 反序列化 DataTable
    /// <summary>
    /// 序列化 对象到字符串
    /// </summary>
    /// <param name="obj">泛型对象</param>
    /// <returns>序列化后的字符串</returns>
    public static string Serialize<T>(T obj)
    {
        try
        {
            IFormatter formatter = new BinaryFormatter();
            MemoryStream stream = new MemoryStream();
            formatter.Serialize(stream, obj);
            stream.Position = 0;
            byte[] buffer = new byte[stream.Length];
            stream.Read(buffer, 0, buffer.Length);
            stream.Flush();
            stream.Close();
            return Convert.ToBase64String(buffer);
        }
        catch (Exception ex)
        {
            throw new Exception("序列化失败,原因:" + ex.Message);
        }
    }

    /// <summary>
    /// 反序列化 字符串到对象
    /// </summary>
    /// <param name="obj">泛型对象</param>
    /// <param name="str">要转换为对象的字符串</param>
    /// <returns>反序列化出来的对象</returns>
    public static T Desrialize<T>(T obj, string str)
    {
        try
        {
            obj = default(T);
            IFormatter formatter = new BinaryFormatter();
            byte[] buffer = Convert.FromBase64String(str);
            MemoryStream stream = new MemoryStream(buffer);
            obj = (T)formatter.Deserialize(stream);
            stream.Flush();
            stream.Close();
        }
        catch (Exception ex)
        {
            throw new Exception("反序列化失败,原因:" + ex.Message);
        }
        return obj;
    }




    /// 序列化DataTable
    /// </summary>
    /// <param name="pDt">包含数据的DataTable</param>
    /// <returns>序列化的DataTable</returns>
    public static string SerializeDataTableXml(DataTable pDt, string pdtname)
    {
        // 序列化DataTable
        pDt.TableName = pdtname;
        StringBuilder sb = new StringBuilder();
        XmlWriter writer = XmlWriter.Create(sb);
        XmlSerializer serializer = new XmlSerializer(typeof(DataTable));
        serializer.Serialize(writer, pDt);
        writer.Close();
        return sb.ToString();
    }


    /// <summary>
    /// 反序列化DataTable
    /// </summary>
    /// <param name="pXml">序列化的DataTable</param>
    /// <returns>DataTable</returns>
    public static DataTable DeserializeDataTable(string pXml)
    {

        StringReader strReader = new StringReader(pXml);
        XmlReader xmlReader = XmlReader.Create(strReader);
        XmlSerializer serializer = new XmlSerializer(typeof(DataTable));

        DataTable dt = serializer.Deserialize(xmlReader) as DataTable;
        return dt;
    }



    #endregion

    //#region XML 文件操作

    ///// <summary>
    ///// 获取 config 
    ///// </summary>
    ///// <param name="config_dir"></param>
    ///// <param name="config_file"></param>
    ///// <returns></returns>
    //public SysConfig GetConfig(string config_dir, string config_file)
    //{
    //    if (!Directory.Exists(config_dir))
    //    {
    //        Directory.CreateDirectory(config_dir);
    //    }
    //    if (!File.Exists(config_file))
    //    {
    //        SysConfig config = new SysConfig();
    //        config.SourceAccount = "sa";
    //        config.SourceDatabaseName = "VideoWatchV3JJ";
    //        config.SourcePWD = "123456";
    //        config.SourceServer = @"192.168.10.11\SQL2008R2";


    //        string res = XmlUtil.Serializer(typeof(SysConfig), config);
    //        File.WriteAllText(config_file, res);

    //    }
    //    string config_xml = File.ReadAllText(config_file);
    //    return (SysConfig)XmlUtil.Deserialize(typeof(SysConfig), config_xml);
    //}

    //public SysConfig GetConfig()
    //{
    //    string config_dir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + @"\config";
    //    config_dir = config_dir.Replace(@"file:\", "");
    //    string config_file = config_dir + "/config.xml";


    //    if (!Directory.Exists(config_dir))
    //    {
    //        Directory.CreateDirectory(config_dir);
    //    }
    //    if (!File.Exists(config_file))
    //    {
    //        SysConfig config = new SysConfig();
    //        config.SourceAccount = "sa";
    //        config.SourceDatabaseName = "VideoWatchV3JJ";
    //        config.SourcePWD = "123456";
    //        config.SourceServer = @"192.168.10.11\SQL2008R2";


    //        string res = XmlUtil.Serializer(typeof(SysConfig), config);
    //        File.WriteAllText(config_file, res);
    //    }
    //    string config_xml = File.ReadAllText(config_file);
    //    return (SysConfig)XmlUtil.Deserialize(typeof(SysConfig), config_xml);
    //}

    ///// <summary>
    ///// 设置 config
    ///// </summary>
    ///// <param name="config"></param>
    ///// <param name="config_file"></param>
    ///// <returns></returns>
    //public int SetConfig(SysConfig config, string config_file)
    //{
    //    string res = XmlUtil.Serializer(typeof(SysConfig), config);
    //    File.WriteAllText(config_file, res);
    //    return 1;
    //}


  
    //public int SetPosConfig(XmlModel config, string config_file)
    //{
    //    string res = XmlUtil.Serializer(typeof(XmlModel), config);
    //    File.WriteAllText(config_file, res);
    //    return 1;
    //}
    //public XmlModel GetPosConfig(string config_dir, string config_file)
    //{
    //    if (!Directory.Exists(config_dir))
    //    {
    //        Directory.CreateDirectory(config_dir);
    //    }
    //    if (!File.Exists(config_file))
    //    {
    //        XmlModel config = new XmlModel();



    //        string res = XmlUtil.Serializer(typeof(XmlModel), config);
    //        File.WriteAllText(config_file, res);

    //    }
    //    string config_xml = File.ReadAllText(config_file);
    //    return (XmlModel)XmlUtil.Deserialize(typeof(XmlModel), config_xml);
    //}
    //public XmlModel GetPosConfig()
    //{
    //    string config_dir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + @"\config";
    //    config_dir = config_dir.Replace(@"file:\", "");
    //    string config_file = config_dir + "/PosConfig.xml";



    //    if (!Directory.Exists(config_dir))
    //    {
    //        Directory.CreateDirectory(config_dir);
    //    }
    //    if (!File.Exists(config_file))
    //    {
    //        XmlModel config = new XmlModel();


    //        string res = XmlUtil.Serializer(typeof(XmlModel), config);
    //        File.WriteAllText(config_file, res);
    //    }
    //    string config_xml = File.ReadAllText(config_file);
    //    return (XmlModel)XmlUtil.Deserialize(typeof(XmlModel), config_xml);
    //}

    //public int SetPosConfig(List<PosFieldDetail> ls, string config_file)
    //{
    //    string res = XmlUtil.Serializer(typeof(List<PosFieldDetail>), ls);
    //    File.WriteAllText(config_file, res);
    //    return 1;
    //}
    //public List<PosFieldDetail> GetPosConfig_1(string config_dir, string config_file)
    //{
    //    if (!Directory.Exists(config_dir))
    //    {
    //        Directory.CreateDirectory(config_dir);
    //    }
    //    if (!File.Exists(config_file))
    //    {
    //        List<PosFieldDetail> ls = new List<PosFieldDetail>();


    //        string res = XmlUtil.Serializer(typeof(SysConfig), ls);
    //        File.WriteAllText(config_file, res);

    //    }
    //    string config_xml = File.ReadAllText(config_file);
    //    return (List<PosFieldDetail>)XmlUtil.Deserialize(typeof(List<PosFieldDetail>), config_xml);
    //}



    //#endregion
}

